package cn.arikeky.order.service.impl;

import cn.arikeky.basic.config.BussinessException;
import cn.arikeky.basic.util.PageList;
import cn.arikeky.order.domain.OrderPetAcquisition;
import cn.arikeky.order.dto.OrderConfirmDto;
import cn.arikeky.order.mapper.OrderPetAcquisitionMapper;
import cn.arikeky.order.service.IOrderPetAcquisitionService;
import cn.arikeky.basic.service.impl.BaseServiceImpl;
import cn.arikeky.pet.domain.Pet;
import cn.arikeky.pet.domain.SearchMasterMsg;
import cn.arikeky.pet.mapper.PetMapper;
import cn.arikeky.pet.mapper.SearchMasterMsgMapper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author arikeky
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;



    @Override
    @Transactional
    public void confirm(OrderConfirmDto orderConfirmDto) {
        // 根据id查询订单
        OrderPetAcquisition orderPetAcquisition = selectById(orderConfirmDto.getId());

        // 查询寻主信息 修改状态 清空shopId
        SearchMasterMsg masterMsg = searchMasterMsgMapper.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (masterMsg == null) {
            throw new BussinessException("寻主消息不存在");
        }
        masterMsg.setState(1);
        masterMsg.setShopId(null);
        searchMasterMsgMapper.update(masterMsg);

        // 保存宠物信息
        Pet pet = initPet(masterMsg);

        // 修改订单状态为完成
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(orderConfirmDto.getMoney());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);


    }



    private Pet initPet(SearchMasterMsg masterMsg) {
        Pet pet = new Pet();
        pet.setName(masterMsg.getName());
        pet.setCostprice(masterMsg.getPrice().toString());
        pet.setTypeId(1L);
        pet.setShopId(masterMsg.getShopId());
        pet.setSearchMasterMsgId(masterMsg.getId());
        petMapper.add(pet);
        return pet;
    }

    @Override
    @Transactional
    public void cancel(Long orderId) {
        // 根据id查询订单
        OrderPetAcquisition orderPetAcquisition = selectById(orderId);
        // 修改状态未取消
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 查询寻主信息 修改状态 清空shopId
        SearchMasterMsg masterMsg = searchMasterMsgMapper.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (masterMsg == null) {
            throw new BussinessException("寻主消息不存在");
        }
        masterMsg.setState(3);
        searchMasterMsgMapper.update(masterMsg);

        // todo 保存日志
    }

    private OrderPetAcquisition selectById(Long orderId) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderId);
        if (orderPetAcquisition == null) {
            throw new BussinessException("订单不存在");
        }
        return orderPetAcquisition;
    }
}
